using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using TelnetExpect;
using System.Threading;
using System.Diagnostics;

namespace TelnetTests
{
    [TestFixture]
    public class TelnetStream_Other_Tests
    {
        byte[] WillEcho = { TestsBase.IAC, TestsBase.WILL, OptionCode.Echo, (byte)'+' };
        byte[] WontEcho = { TestsBase.IAC, TestsBase.WONT, OptionCode.Echo, (byte)'-' };

        [Test]
        public void IsRemoteEcho()
        {
            FooStream foo = new FooStream();
            Thread t =null;
            try {
                using (TelnetStream telnet = new TelnetStream(foo)) {
                    t = new Thread(ReadForever);
                    t.IsBackground = true;
                    t.Start(telnet);
                    //
                    Assert.IsFalse(telnet.IsRemoteEcho, "1");
                    foo.SendToReadAndWaitDidWrite(WillEcho);
                    Assert.IsTrue(telnet.IsRemoteEcho, "2");
                    foo.SendToReadAndWaitDidWrite(WontEcho);
                    Assert.IsFalse(telnet.IsRemoteEcho, "3");
                }
            } finally {
                if (t != null) {
                    t.Abort();
                }
            }
        }

        void ReadForever(object state)
        {
            TelnetStream telnet = (TelnetStream)state;
            byte[] buf = new byte[100];
            while (true) {
                int readLen = telnet.Read(buf, 0, buf.Length);
                if (readLen == 0) {
                    break;
                }
                Debug.Assert(readLen == 1);
            }//while
        }

    }
}
